var UserData = require('UserData')
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        coin1: {
            default: null,
            type: cc.Prefab,
        },
        coin2: {
            default: null,
            type: cc.Prefab,
        },
        coin3: {
            default: null,
            type: cc.Prefab,
        },
        coin4: {
            default: null,
            type: cc.Prefab,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },
    popWindow(money) {
        this.physicsManager = cc.director.getPhysicsManager()
        this.coinActionBox = this.node.getChildByName('框1').getChildByName('金币动作')
        this.offlineGainLabel = this.node.getChildByName('框1').getChildByName('收益')
        this.node.active = true
        this.node.scaleX = 0
        this.node.scaleY = 0
        this.node.active = true
        this.node.runAction(cc.scaleTo(0.3, 1, 1).easing(cc.easeBackOut()))
        //this.physicsManager.enabled = true
        this.money = money * (UserData.offlineGainRatio?UserData.offlineGainRatio:COMMON_VALUE.OFFLINE_GAIN_RATIO)////一个离线收益的缩小百分比0~1之间
        this.offlineGainLabel.getComponent('labelFixed').setNumber(money, '您赚到了\n','金币')
        this.rate = Math.floor(Math.random()*5+2)
        this.node.getChildByName('开始交易按钮').getChildByName('开始交易').getComponent(cc.Label).string = '收益x'+ this.rate 
    },

    closeWindow() {
        UserData.addCurrentMoney(this.money)
        this.node.runAction(cc.sequence(cc.scaleTo(0.3, 0, 0).easing(cc.easeBackIn()), cc.callFunc((self) => {
            self.active = false
        }, this)))

    },
    confirmGain(event, rate) {
        // 如果没传rate参数进来, 默认为分享, 否则1倍率离线收益
        rate = rate ? 1 : this.rate
        if (rate != 1) {
            try {
                let setting = {
                    title: '你禁得住诱惑吗？',
                    imageUrl: 'http://img.wenzhangba.com/allimg/180806/1401-1PP60524130.png',
                    query: 'whoami=bbbbt'
                }

                if (UserData.runtimeState && UserData.runtimeState.shareSettings) {
                    let randomIndex = Math.floor(Math.random() * UserData.runtimeState.shareSettings.length)
                    setting = UserData.runtimeState.shareSettings[randomIndex]
                }
                wx.shareAppMessage(setting)
            } catch (err) {
                console.log(err)
            }
        }
        
        UserData.addCurrentMoney(this.money * parseInt(rate))
        this.money = 0
        this.node.getChildByName('开始交易按钮').getComponent(cc.Button).interactable = false
        this.closeWindow()
    }
    // update (dt) {},
});
